/**
 * 
 */
package furniture.items3d;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class Visitable {

	/**
	 * Accept a visitor. To do this, we must find a
	 * corresponding visit(X) method in the visitor object
	 * @param visitor
	 */
	public void accept(Visitor visitor){
		Class<?> visitorClass = visitor.getClass();
		Class<?> thisClass = this.getClass();
		try {
			Method method = visitorClass.getMethod("visit", thisClass);
			method.setAccessible(true);
			method.invoke(visitor, this);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Accept a visitor. To do this, we must find a
	 * corresponding visitEx(X) method in the visitor object.
	 * This method returns the result of the visitEx(x) method.
	 * @param visitor
	 */
	public Object acceptEx(Visitor visitor){
		Class<?> visitorClass = visitor.getClass();
		Class<?> thisClass = this.getClass();
		try {
			Method method = visitorClass.getMethod("visitEx", thisClass);
			method.setAccessible(true);
			return method.invoke(visitor, this);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
